﻿using System.Configuration;
using System.Data;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Newtonsoft.Json;
using Wantn.Core;
using Wantn.DAS.Msg.Common;
using Wantn.WinFormSys;
namespace Wantn.LMS
{
    /// <summary>
    /// 化验接收
    /// </summary>
    public partial class FrmRecive : FrmBase
    {

        private static readonly string StrUrl = ConfigurationManager.AppSettings["Url"];
        /// <summary>
        /// 默认构造方法
        /// </summary>
        public FrmRecive()
        {
            InitializeComponent();
        }

        private void ultraTool_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Recive":
                    DoRecive();
                    break;
                case "Query":
                    DoQuery();
                    break;
            }
        }

        private void DoRecive()
        {
            var obj = new FrmReciveSample {Url = StrUrl};
            if (obj.ShowDialog() == DialogResult.OK)
            {
                DoQuery();
            }
        }

        private void DoQuery()
        {
            dataTable1.Rows.Clear();

            #region 初始化变量
            var url = StrUrl + "/common/querysql.do";
            const string strSql = "select t.f_id,t.f_sid,t.f_scode,t.f_suid,t.f_stime,t.f_code from T_S_RECEIVE t ";
            #endregion

            #region 调用中间件返回查询结果
            var msg = new DasMsgQuerySql(strSql);
            var reqContentPlain = JsonConvert.SerializeObject(msg);
            //发送POST请求获得响应数据
            var result = DasClinet.SendPost(url, 30 * 1000, true, UserInfo.UserName, UserInfo.UserPwd, reqContentPlain);
            if (!result.IsSuccess || result.StatusCode != 1)
            {
                MessageBox.Show(@"查询数据失败" + "\n错误1：" + result.StatusDesc + "\n错误2：" + result.ErrInfo);
                return;
            }
            //反序列响应数据到具体的业务响应实体对象
            var jsonSet = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            msg = JsonConvert.DeserializeObject<DasMsgQuerySql>(result.ResponseContentPlain, jsonSet);
            #endregion

            #region 显示查询结果
            if (msg.ResultDataset == null || msg.ResultDataset.Tables.Count <= 0 ||
                msg.ResultDataset.Tables[0].Rows.Count <= 0) return;

            foreach (DataRow row in msg.ResultDataset.Tables[0].Rows)
            {
                var newRow = dataTable1.NewRow();
                foreach (DataColumn column in dataTable1.Columns)
                {
                    newRow[column.ColumnName] = row[column.ColumnName];
                }
                dataTable1.Rows.Add(newRow);
            }
            dataTable1.AcceptChanges();
            //UltraGrid自适应宽度
            utg_Main.DisplayLayout.Bands[0].PerformAutoResizeColumns(false, PerformAutoSizeType.AllRowsInBand);
            #endregion
        }
    }
}
